package com.songcha.library_business.proxy.userinfo

import com.alibaba.android.arouter.launcher.ARouter
import com.google.gson.Gson
import com.songcha.library_business.bean.user.UserInfoBean
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_common.LibraryCommon
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.library_common.util.TimeUtil
import com.songcha.library_common.util.ToastUtil

class UserInfoManager {
    companion object{
        const val LOGIN_FILE_NAME="login"
         var isLogin=false
        var loginToken=""
        var userInfo:UserInfoBean.DataBean?=null
        var userId=0
        var vipValidateTime=0L


        @JvmStatic
        fun init(){
            isLogin=SharedPreferenceHelper.getInt(LOGIN_FILE_NAME,"is_login")==1
            loginToken=SharedPreferenceHelper.getString(LOGIN_FILE_NAME,"login_token")
            if(isLogin && loginToken.isNotBlank()){
                isLogin=true
                val str=SharedPreferenceHelper.getString(LOGIN_FILE_NAME,"user_info")
                if(str.isNotBlank()){
                    try{
                        userInfo=Gson().fromJson(str,UserInfoBean.DataBean::class.java)
                        userId= userInfo!!.id
                        vipValidateTime=TimeUtil.timeString2Stamp(userInfo!!.vipLastTime)
                    }catch (e:Exception){
                        e.printStackTrace()
                        clearLogin()
                    }

                }
            }else{
                isLogin=false
            }

        }

        @JvmStatic
        fun saveLogin(token:String){
            isLogin=true
            loginToken=token
            SharedPreferenceHelper.setInt(LOGIN_FILE_NAME,"is_login",1)
            SharedPreferenceHelper.setString(LOGIN_FILE_NAME,"login_token",token)
        }

        @JvmStatic
        fun saveUserInfo(userInfo:UserInfoBean.DataBean){
            userId=userInfo.id
            this.userInfo=userInfo
            vipValidateTime=TimeUtil.timeString2Stamp(userInfo.vipLastTime)
            SharedPreferenceHelper.setString(LOGIN_FILE_NAME,"user_info",Gson().toJson(userInfo))
        }

        @JvmStatic
        fun clearLogin(){
            isLogin=false
            loginToken=""
            userInfo=null
            userId=0
            vipValidateTime=0L
            SharedPreferenceHelper.setInt(LOGIN_FILE_NAME,"is_login",0)
            SharedPreferenceHelper.setString(LOGIN_FILE_NAME,"login_token","")
            SharedPreferenceHelper.setString(LOGIN_FILE_NAME,"user_info","")
        }

        @JvmStatic
        fun checkLogin():Boolean{
            return isLogin && loginToken.isNotBlank() && userInfo!=null
        }

        @JvmStatic
        fun checkJumpLogin():Boolean{
            if(!isLogin || loginToken.isBlank() || userInfo==null){
                BusinessJumpUtil.toLoginActivity(LibraryCommon.getContext())
                //ToastUtil.show("请先登录")
                return false
            }
            return true
        }

        @JvmStatic
        fun isVip():Boolean{
            return isLogin && loginToken.isNotBlank() && userInfo!=null && vipValidateTime>System.currentTimeMillis()
        }

        //whether is vip once,but already expire
        @JvmStatic
        fun isOnceVip():Boolean{
            return isLogin && loginToken.isNotBlank() && userInfo!=null && vipValidateTime>0 && vipValidateTime<=System.currentTimeMillis()
        }

        @JvmStatic
        fun isTodaySignIn():Boolean{
            if(!checkLogin()) return false
            val lastSignTime = TimeUtil.timeString2Stamp(userInfo!!.lastSigninDate)/1000
            return lastSignTime>0 && lastSignTime>=TimeUtil.getTodayZeroTimeStamp()
        }

    }
}